﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace YJY_DMZS.BasisFile
{
    public partial class FrmProductType_Editor : XtraForm 
    {
        /// <summary>
        /// 主键值，非正零为新增
        /// </summary>
        public long ID;
        /// <summary>
        /// 父类型节点
        /// </summary>
        public long ParentID;
        /// <summary>
        /// 标题栏文字格式
        /// </summary>
        private string captionFormat;

        #region ================================初始化=====================================
        public FrmProductType_Editor()
        {
            InitializeComponent();
        }

        private void FrmProductType_Editor_Load(object sender, EventArgs e)
        {
            captionFormat = Text;
        }

        private void FrmProductType_Editor_Shown(object sender, EventArgs e)
        {
            if (ID <= 0)
            {
                NewRecord();
            }
            else
            {
                LoadData(ID);
            }
        }

        //enter -> tab
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Enter && !(this.ActiveControl.Parent is ButtonEdit))
            {
                keyData = Keys.Tab;
            }
            return base.ProcessDialogKey(keyData);
        }

        #endregion ================================初始化=====================================


        #region ================================工具条按钮事件=====================================
        //新增
        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (ID <= 0) return;
            NewRecord();
        }

        //保存
        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (txtCode.Text.Trim().Length == 0)
            {
                XtraMessageBox.Show("请输入类型编号。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtCode.Focus();
                return;
            }
            else
            {
                if (txtName.Text.Trim().Length == 0)
                {
                    XtraMessageBox.Show("请输入类型名称。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    txtName.Focus();
                    return;
                }
            }
            if (ID <= 0)
            {
                SaveNew();
            }
            else
            {
                SaveModify();
            }
        }

        //删除
        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var bll = new DMZS.BLL.ProductType();

            var childrenCount = bll.GetChildrenCount(ID);
            if (childrenCount < 0)
            {
                XtraMessageBox.Show("检索子级数据出错，建议刷新数据。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                return;
            }

            var text = string.Empty;
            if (childrenCount != 0)
            {
                text = string.Format("确认删除,其下有{0}子项？\r注意：将同时删除子项！", childrenCount);
            }
            else
            {
                text = "确认删除？";
            }

            if (DialogResult.No == XtraMessageBox.Show(text, Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
            {
                return;
            }

            int result = bll.DeleteExpand(Convert.ToInt64(ID));
            if (result > 0)
            {
                NewRecord();
                //XtraMessageBox.Show("删除成功。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
            else if (result == -1)
            {
                XtraMessageBox.Show("删除失败，建议刷新数据。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                return;
            }
            else if (result == -2)
            {
                XtraMessageBox.Show("该类别或其子类别已被引用，无法删除。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                return;
            }
        }

        //刷新
        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (ID <= 0)
            {
                return;
            }
            LoadData(ID);
        }
        #endregion ================================工具条按钮事件=====================================


        #region ================================功能函数区=====================================

        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="ProductTypeID"></param>
        private void LoadData(long ProductTypeID)
        {
            Text = string.Format(captionFormat, "维护");
            var bll = new DMZS.BLL.ProductType();
            var dt = bll.GetRecordByID(ProductTypeID);
            if (dt != null)
            {
                ParentID = Convert.ToInt64(dt.Rows[0]["ParentID"]);
                txtParentCode.Text = dt.Rows[0]["ParentCode"].ToString();
                if (ParentID == 0)
                {
                    txtParentName.Text = "(顶级)";
                }
                else
                {
                    txtParentName.Text = dt.Rows[0]["ParentName"].ToString();
                }
                txtCode.Text = dt.Rows[0]["Code"].ToString();
                txtName.Text = dt.Rows[0]["Name"].ToString();
                txtCode.Properties.ReadOnly = true;
                txtName.Focus();
            }
            else
            {
                XtraMessageBox.Show("该条数据已被删除，请刷新数据。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                Close();
            }
        }

        /// <summary>
        /// 新记录
        /// </summary>
        private void NewRecord()
        {
            Text = string.Format(captionFormat, "新增");
            ID = 0;
            txtCode.Properties.ReadOnly = false;

            if (ParentID != 0)
            {
                var bll = new DMZS.BLL.ProductType();
                var model = bll.GetModel(ParentID);

                if (model != null)
                {
                    txtParentCode.Text = model.Code;
                    txtParentName.Text = model.Name;
                    txtCode.Text = string.Empty;
                    txtName.Text = string.Empty;
                }
                else
                {
                    ParentID = 0;
                    txtParentCode.Text = "";
                    txtParentName.Text = "(顶级)";
                    txtCode.Text = string.Empty;
                    txtName.Text = string.Empty;
                }
            }
            else
            {
                txtParentCode.Text = string.Empty;
                txtParentName.Text = "(顶级)";
                txtCode.Text = string.Empty;
                txtName.Text = string.Empty;
            }

            txtCode.Focus();
        }

        /// <summary>
        /// 保存修改
        /// </summary>
        private void SaveModify()
        {
            var bll = new DMZS.BLL.ProductType();
            var model = new DMZS.Model.ProductType();
            model.ID = ID;
            model.Code = txtCode.Text.Trim();
            model.Name = txtName.Text.Trim();
            model.ParentID = ParentID;

            if (bll.Update(model))
            {
                XtraMessageBox.Show("保存成功。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
            else
            {
                XtraMessageBox.Show("保存失败。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                Close();
                return;
            }
            LoadData(ID);
        }
        /// <summary>
        /// 保存新增
        /// </summary>
        private void SaveNew()
        {
            var bll = new DMZS.BLL.ProductType();
            var model = new DMZS.Model.ProductType();
            model.Code = txtCode.Text.Trim();
            model.Name = txtName.Text.Trim();
            model.ParentID = ParentID;
            var resutl = bll.AddExpand(model, ParentID);
            if (resutl > 0)
            {
                XtraMessageBox.Show("保存成功。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                ID = resutl;
                LoadData(ID);
            }
            else
            {
                if (resutl == -1001)
                {
                    XtraMessageBox.Show("该类型编号重复。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    txtCode.Focus();
                    txtCode.SelectAll();
                }
                else
                {
                    XtraMessageBox.Show("保存失败，建议刷新数据。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                }
            }
        }
        #endregion ================================功能函数区=====================================

    }
}
